package com.lanjiu.lib.imclient.ims.offline;

import com.lanjiu.lib.communication.IMSConfig;
import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.lanjiu.lib.imclient.ims.IMSUtils;
import com.lanjiu.lib.imclient.user.UserPreferences;
import com.lanjiu.lib.utils.CollectionUtils;
import com.lanjiu.pro.business.BusinessProtocolEntities;
import com.lanjiu.pro.business.BusinessProtocolMessageStandard;
import com.lanjiu.pro.business.BusinessProtocolMessages;
import com.ljkj.cordial.chat.ui.ChatUtils;
import com.ljkj.cordial.lengthtcpheartbeat.OnDispenseResponseListener;
import com.ljkj.cordial.lengthtcpheartbeat.TcpLengthHeartbeatBroadcastReceive;

import java.util.List;

/**
 * @description:
 * @author: Panwei
 * @date: 2019/12/21 10:57
 */
public class IMSOfflineMessageHandler {

    IMSOfflineMessageHandler() {
    }

    public static boolean isOfflineResponse(String msgType) {
        return (IMSContacts.MsgType.GET_OFF_MESSAGE_REGIST.equals(msgType) ||
                IMSContacts.MsgType.GET_OFF_MESSAGE_TOURIST.equals(msgType) ||
                IMSContacts.MsgType.GROUP_CHART_OFF_LINE_MSG.equals(msgType));
    }

    public static boolean isOfflineReceipt(BusinessProtocolMessageStandard.UnifiedEntranceMessage msg) {
        String msgType = msg.getHead().getMsgType();
        if (IMSConfig.STATUS_REPORT_SUCCESS.equals(msg.getHead().getStatusReport())) {
            if (IMSContacts.MsgType.GROUP_CHART_OFF_LINE_MSG.equals(msgType) && msg.getOfflineGroupMessageProtocol() != null && !CollectionUtils.isEmpty(msg.getOfflineGroupMessageProtocol().getGroupOfflineMessageList())) {
                return msg.getOfflineGroupMessageProtocol().getRequest();
            } else if (msg.getOffFriendMessageListProtocol() != null &&
                    ((IMSContacts.MsgType.GET_OFF_MESSAGE_REGIST.equals(msgType) && !CollectionUtils.isEmpty(msg.getOffFriendMessageListProtocol().getRegistFriendOffMessageList())) || (IMSContacts.MsgType.GET_OFF_MESSAGE_TOURIST.equals(msgType) && !CollectionUtils.isEmpty(msg.getOffFriendMessageListProtocol().getTouristFriendOffMessageList())))) {
                return msg.getOffFriendMessageListProtocol().getRequest();
            }
        }
        return true;
    }

    public static void handleGroupOfflineMessage(List<BusinessProtocolEntities.GroupOfflineMessage> groupOfflineMessage) {
        OnDispenseResponseListener onDispenseResponseListener = TcpLengthHeartbeatBroadcastReceive.getOnDispenseResponseListener();
        if (onDispenseResponseListener == null) return;

        for (BusinessProtocolEntities.GroupOfflineMessage offlineMessage : groupOfflineMessage) {
            BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage cMsg = handleGroupOfflineMessage(offlineMessage);
            if (null != cMsg) {
                onDispenseResponseListener.onResponse(cMsg.getUnifiedEntranceMessage().getHead().getMsgType(), cMsg);
            }
        }
    }

    private static BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage handleGroupOfflineMessage(BusinessProtocolEntities.GroupOfflineMessage offlineMessage) {
        if (offlineMessage.getOfflineMessageType() == IMSContacts.OffLineMsgType.TEXT ||
                offlineMessage.getOfflineMessageType() == IMSContacts.OffLineMsgType.PICTURE ||
                offlineMessage.getOfflineMessageType() == IMSContacts.OffLineMsgType.VIDEO ||
                offlineMessage.getOfflineMessageType() == IMSContacts.OffLineMsgType.VOICE ||
                offlineMessage.getOfflineMessageType() == IMSContacts.OffLineMsgType.FILE) {
            BusinessProtocolEntities.ChatMessage chatMessage = offlineMessage.getChatMessage();
            if (chatMessage == null) return null;
            boolean isFromRegisteredUser = UserPreferences.isRegistered(IMSUtils.userTypeAnalysis(offlineMessage.getMsgFromId()));
            String msgType = isFromRegisteredUser ? IMSContacts.MsgType.GROUP_CHAT_REGISTERED : IMSContacts.MsgType.GROUP_CHAT_TOURISTS;
            BusinessProtocolMessageStandard.Head head = BusinessProtocolMessageStandard.Head.newBuilder()
                    .setMsgType(msgType)
                    .setFromId(offlineMessage.getMsgFromId())
                    .setToId(offlineMessage.getGroupId())
                    .setTimestamp(ChatUtils.analysisTimestampInMessageId(chatMessage.getMsgId()))
                    .setUniqueIdentify(chatMessage.getMsgId())
                    .build();
            BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage.Builder cMsgBuilder = BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage.newBuilder();
            if (isFromRegisteredUser) {
                cMsgBuilder.setUnifiedEntranceMessage(BusinessProtocolMessageStandard.UnifiedEntranceMessage.newBuilder()
                        .setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.GroupChatProtocol)
                        .setHead(head)
                        .setGroupChatProtocol(BusinessProtocolMessages.GroupChatProtocol.newBuilder()
                                .setUserType(IMSContacts.UserType.REGISTERED_USER)
                                .setRegisteredGroupMember(BusinessProtocolEntities.RegisteredGroupMember.newBuilder()
                                        .setGroupMemberUserId(offlineMessage.getGroupMemberUserId())
                                        .setGroupMemberName(offlineMessage.getGroupMemberNickname())
                                        .setGroupMemberAvatar(offlineMessage.getGroupMemberAvatar())
                                        .setGroup(BusinessProtocolEntities.RegisteredGroup.newBuilder()
                                                .setGroupId(offlineMessage.getGroupId())
                                                .setGroupName(offlineMessage.getGroupName())
                                                .setGroupType(IMSUtils.userTypeAnalysis(offlineMessage.getGroupId()))
                                                .setSettings(BusinessProtocolEntities.Settings.newBuilder()
                                                        .setSnapchatStatus(offlineMessage.getSnapchatStatus())
                                                        .setSnapchatTimeLength(offlineMessage.getSnapchatTimeLength())
                                                        .build())
                                                .build())
                                        .build())
                                .setAt(offlineMessage.getAt())
                                .setChatMessage(chatMessage)
                                .build())
                        .build());
            } else {
                cMsgBuilder.setUnifiedEntranceMessage(BusinessProtocolMessageStandard.UnifiedEntranceMessage.newBuilder()
                        .setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.GroupChatProtocol)
                        .setHead(head)
                        .setGroupChatProtocol(BusinessProtocolMessages.GroupChatProtocol.newBuilder()
                                .setUserType(IMSContacts.UserType.TOURISTS_USER)
                                .setTouristsGroupMember(BusinessProtocolEntities.TouristsGroupMember.newBuilder()
                                        .setGroupMemberUserId(offlineMessage.getGroupMemberUserId())
                                        .setGroupMemberName(offlineMessage.getGroupMemberNickname())
                                        .setGroupMemberAvatar(offlineMessage.getGroupMemberAvatar())
                                        .setGroup(BusinessProtocolEntities.TouristsGroup.newBuilder()
                                                .setGroupId(offlineMessage.getGroupId())
                                                .setGroupName(offlineMessage.getGroupName())
                                                .setGroupType(IMSUtils.userTypeAnalysis(offlineMessage.getGroupId()))
                                                .setSettings(BusinessProtocolEntities.Settings.newBuilder()
                                                        .setSnapchatStatus(offlineMessage.getSnapchatStatus())
                                                        .setSnapchatTimeLength(offlineMessage.getSnapchatTimeLength())
                                                        .build())
                                                .build())
                                        .build())
                                .setAt(offlineMessage.getAt())
                                .setChatMessage(chatMessage)
                                .build())
                        .build());
            }
            return cMsgBuilder.build();
        } else if (offlineMessage.getOfflineMessageType() == IMSContacts.OffLineMsgType.CREATE) {
            // 创建群
            boolean isFromRegisteredUser = UserPreferences.isRegistered(IMSUtils.userTypeAnalysis(offlineMessage.getMsgFromId()));
            String msgType = isFromRegisteredUser ? IMSContacts.MsgType.GROUP_CREATE_REGISTERED : IMSContacts.MsgType.GROUP_CREATE_TOURISTS;
            BusinessProtocolMessageStandard.Head head = BusinessProtocolMessageStandard.Head.newBuilder()
                    .setMsgType(msgType)
                    .setFromId(offlineMessage.getMsgFromId())
                    .setToId(UserPreferences.getUserId())
                    .setTimestamp(System.currentTimeMillis())
                    .build();
            BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage.Builder cMsgBuilder = BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage.newBuilder();
            if (isFromRegisteredUser) {
                cMsgBuilder.setUnifiedEntranceMessage(BusinessProtocolMessageStandard.UnifiedEntranceMessage.newBuilder()
                        .setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.GroupCreateProtocol)
                        .setHead(head)
                        .setGroupCreateProtocol(BusinessProtocolMessages.GroupCreateProtocol.newBuilder()
                                .setUserType(IMSContacts.UserType.REGISTERED_USER)
                                .setRegisteredUser(BusinessProtocolEntities.RegisteredUser.newBuilder().setUserId(UserPreferences.getUserId()).build())
                                .setRegisteredGroup(BusinessProtocolEntities.RegisteredGroup.newBuilder()
                                        .setGroupId(offlineMessage.getGroupId())
                                        .setGroupName(offlineMessage.getGroupName())
                                        .build())
                                .build())
                        .build());
            } else {
                cMsgBuilder.setUnifiedEntranceMessage(BusinessProtocolMessageStandard.UnifiedEntranceMessage.newBuilder()
                        .setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.GroupCreateProtocol)
                        .setHead(head)
                        .setGroupCreateProtocol(BusinessProtocolMessages.GroupCreateProtocol.newBuilder()
                                .setUserType(IMSContacts.UserType.TOURISTS_USER)
                                .setTouristsUser(BusinessProtocolEntities.TouristsUser.newBuilder().setTemporaryId(UserPreferences.getUserId()).build())
                                .setTouristsGroup(BusinessProtocolEntities.TouristsGroup.newBuilder()
                                        .setGroupId(offlineMessage.getGroupId())
                                        .setGroupName(offlineMessage.getGroupName())
                                        .build())
                                .build())
                        .build());
            }
            return cMsgBuilder.build();
        } else if (offlineMessage.getOfflineMessageType() == IMSContacts.OffLineMsgType.INVITE_IN) {
            // 邀请入群
            boolean isFromRegisteredUser = UserPreferences.isRegistered(IMSUtils.userTypeAnalysis(offlineMessage.getMsgFromId()));
            String msgType = isFromRegisteredUser ? IMSContacts.MsgType.GROUP_MEMBER_INVITE_REGISTERED : IMSContacts.MsgType.GROUP_MEMBER_INVITE_TOURISTS;
            BusinessProtocolMessageStandard.Head head = BusinessProtocolMessageStandard.Head.newBuilder()
                    .setMsgType(msgType)
                    .setFromId(offlineMessage.getMsgFromId())
                    .setToId(UserPreferences.getUserId())
                    .setTimestamp(System.currentTimeMillis())
                    .build();
            BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage.Builder cMsgBuilder = BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage.newBuilder();
            int groupId;
            int userId;
            try {
                groupId = Integer.parseInt(offlineMessage.getGroupId());
                userId = Integer.parseInt(offlineMessage.getUserId());
            } catch (NumberFormatException e) {
                return null;
            }
            if (isFromRegisteredUser) {
                cMsgBuilder.setUnifiedEntranceMessage(BusinessProtocolMessageStandard.UnifiedEntranceMessage.newBuilder()
                        .setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.GroupMemberAddProtocol)
                        .setHead(head)
                        .setGroupMemberAddProtocol(BusinessProtocolMessages.GroupMemberAddProtocol.newBuilder()
                                .setUserType(IMSContacts.UserType.REGISTERED_USER)
                                .addAllGroupMemberVo(BusinessProtocolEntities.AllGroupMemberVO.newBuilder()
                                        .setGroupEntryTime(0)
                                        .setRegisteredGroupId(groupId)
                                        .setRegisteredUserId(userId)
                                        .build())
                                .build())
                        .build());
            } else {
                cMsgBuilder.setUnifiedEntranceMessage(BusinessProtocolMessageStandard.UnifiedEntranceMessage.newBuilder()
                        .setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.GroupMemberAddProtocol)
                        .setHead(head)
                        .setGroupMemberAddProtocol(BusinessProtocolMessages.GroupMemberAddProtocol.newBuilder()
                                .setUserType(IMSContacts.UserType.TOURISTS_USER)
                                .addAllGroupMemberVo(BusinessProtocolEntities.AllGroupMemberVO.newBuilder()
                                        .setGroupEntryTime(0)
                                        .setGuestGroupId(groupId)
                                        .setGuestUserId(userId)
                                        .build())
                                .build())
                        .build());
            }
            return cMsgBuilder.build();
        } else if (offlineMessage.getOfflineMessageType() == IMSContacts.OffLineMsgType.REQ_JOIN_IN) {
            // 申请入群
            boolean isFromRegisteredUser = UserPreferences.isRegistered(IMSUtils.userTypeAnalysis(offlineMessage.getMsgFromId()));
            String msgType = isFromRegisteredUser ? IMSContacts.MsgType.GROUP_MEMBER_APPLY_REGISTERED : IMSContacts.MsgType.GROUP_MEMBER_APPLY_TOURISTS;
            BusinessProtocolMessageStandard.Head head = BusinessProtocolMessageStandard.Head.newBuilder()
                    .setMsgType(msgType)
                    .setFromId(offlineMessage.getMsgFromId())
                    .setToId(UserPreferences.getUserId())
                    .setTimestamp(System.currentTimeMillis())
                    .build();
            BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage.Builder cMsgBuilder = BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage.newBuilder();
            if (isFromRegisteredUser) {
                cMsgBuilder.setUnifiedEntranceMessage(BusinessProtocolMessageStandard.UnifiedEntranceMessage.newBuilder()
                        .setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.GroupChatProtocol)
                        .setHead(head)
                        .setGroupChatProtocol(BusinessProtocolMessages.GroupChatProtocol.newBuilder()
                                .setUserType(IMSContacts.UserType.REGISTERED_USER)
                                .setChatMessage(offlineMessage.getChatMessage())
                                .setRegisteredGroupMember(BusinessProtocolEntities.RegisteredGroupMember.newBuilder()
                                        .setGroupMemberUserId(offlineMessage.getGroupMemberUserId())
                                        .setGroupMemberName(offlineMessage.getGroupMemberNickname())
                                        .setGroupMemberAvatar(offlineMessage.getGroupMemberAvatar())
                                        .setGroup(BusinessProtocolEntities.RegisteredGroup.newBuilder()
                                                .setGroupId(offlineMessage.getGroupId())
                                                .setGroupName(offlineMessage.getGroupName())
                                                .setGroupType(IMSUtils.userTypeAnalysis(offlineMessage.getGroupId()))
                                                .setSettings(BusinessProtocolEntities.Settings.newBuilder()
                                                        .setSnapchatStatus(offlineMessage.getSnapchatStatus())
                                                        .setSnapchatTimeLength(offlineMessage.getSnapchatTimeLength())
                                                        .build())
                                                .build())
                                        .build())
                                .build())
                        .build());
            } else {
                cMsgBuilder.setUnifiedEntranceMessage(BusinessProtocolMessageStandard.UnifiedEntranceMessage.newBuilder()
                        .setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.GroupChatProtocol)
                        .setHead(head)
                        .setGroupChatProtocol(BusinessProtocolMessages.GroupChatProtocol.newBuilder()
                                .setUserType(IMSContacts.UserType.TOURISTS_USER)
                                .setChatMessage(offlineMessage.getChatMessage())
                                .setTouristsGroupMember(BusinessProtocolEntities.TouristsGroupMember.newBuilder()
                                        .setGroupMemberUserId(offlineMessage.getGroupMemberUserId())
                                        .setGroupMemberName(offlineMessage.getGroupMemberNickname())
                                        .setGroupMemberAvatar(offlineMessage.getGroupMemberAvatar())
                                        .setGroup(BusinessProtocolEntities.TouristsGroup.newBuilder()
                                                .setGroupId(offlineMessage.getGroupId())
                                                .setGroupName(offlineMessage.getGroupName())
                                                .setGroupType(IMSUtils.userTypeAnalysis(offlineMessage.getGroupId()))
                                                .setSettings(BusinessProtocolEntities.Settings.newBuilder()
                                                        .setSnapchatStatus(offlineMessage.getSnapchatStatus())
                                                        .setSnapchatTimeLength(offlineMessage.getSnapchatTimeLength())
                                                        .build())
                                                .build())
                                        .build())
                                .build())
                        .build());
            }
            return cMsgBuilder.build();
        } else if (offlineMessage.getOfflineMessageType() == IMSContacts.OffLineMsgType.REQ_REFUSE) {
            // 申请拒绝
            boolean isFromRegisteredUser = UserPreferences.isRegistered(IMSUtils.userTypeAnalysis(offlineMessage.getMsgFromId()));
            String msgType = isFromRegisteredUser ? IMSContacts.MsgType.GROUP_MEMBER_APPLY_REFUSE_REGISTERED : IMSContacts.MsgType.GROUP_MEMBER_APPLY_REFUSE_TOURISTS;
            BusinessProtocolMessageStandard.Head head = BusinessProtocolMessageStandard.Head.newBuilder()
                    .setMsgType(msgType)
                    .setFromId(offlineMessage.getMsgFromId())
                    .setToId(UserPreferences.getUserId())
                    .setTimestamp(System.currentTimeMillis())
                    .build();
            BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage.Builder cMsgBuilder = BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage.newBuilder();
            if (isFromRegisteredUser) {
                cMsgBuilder.setUnifiedEntranceMessage(BusinessProtocolMessageStandard.UnifiedEntranceMessage.newBuilder()
                        .setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.GroupChatProtocol)
                        .setHead(head)
                        .setGroupChatProtocol(BusinessProtocolMessages.GroupChatProtocol.newBuilder()
                                .setUserType(IMSContacts.UserType.REGISTERED_USER)
                                .setChatMessage(offlineMessage.getChatMessage())
                                .setRegisteredGroupMember(BusinessProtocolEntities.RegisteredGroupMember.newBuilder()
                                        .setGroupMemberUserId(offlineMessage.getGroupMemberUserId())
                                        .setGroupMemberName(offlineMessage.getGroupMemberNickname())
                                        .setGroupMemberAvatar(offlineMessage.getGroupMemberAvatar())
                                        .setGroup(BusinessProtocolEntities.RegisteredGroup.newBuilder()
                                                .setGroupId(offlineMessage.getGroupId())
                                                .setGroupName(offlineMessage.getGroupName())
                                                .setGroupType(IMSUtils.userTypeAnalysis(offlineMessage.getGroupId()))
                                                .setSettings(BusinessProtocolEntities.Settings.newBuilder()
                                                        .setSnapchatStatus(offlineMessage.getSnapchatStatus())
                                                        .setSnapchatTimeLength(offlineMessage.getSnapchatTimeLength())
                                                        .build())
                                                .build())
                                        .build())
                                .build())
                        .build());
            } else {
                cMsgBuilder.setUnifiedEntranceMessage(BusinessProtocolMessageStandard.UnifiedEntranceMessage.newBuilder()
                        .setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.GroupChatProtocol)
                        .setHead(head)
                        .setGroupChatProtocol(BusinessProtocolMessages.GroupChatProtocol.newBuilder()
                                .setUserType(IMSContacts.UserType.TOURISTS_USER)
                                .setChatMessage(offlineMessage.getChatMessage())
                                .setTouristsGroupMember(BusinessProtocolEntities.TouristsGroupMember.newBuilder()
                                        .setGroupMemberUserId(offlineMessage.getGroupMemberUserId())
                                        .setGroupMemberName(offlineMessage.getGroupMemberNickname())
                                        .setGroupMemberAvatar(offlineMessage.getGroupMemberAvatar())
                                        .setGroup(BusinessProtocolEntities.TouristsGroup.newBuilder()
                                                .setGroupId(offlineMessage.getGroupId())
                                                .setGroupName(offlineMessage.getGroupName())
                                                .setGroupType(IMSUtils.userTypeAnalysis(offlineMessage.getGroupId()))
                                                .setSettings(BusinessProtocolEntities.Settings.newBuilder()
                                                        .setSnapchatStatus(offlineMessage.getSnapchatStatus())
                                                        .setSnapchatTimeLength(offlineMessage.getSnapchatTimeLength())
                                                        .build())
                                                .build())
                                        .build())
                                .build())
                        .build());
            }
            return cMsgBuilder.build();
        } else if (offlineMessage.getOfflineMessageType() == IMSContacts.OffLineMsgType.REQ_CONSENT) {
            // 申请同意
            // 邀请默认同意，，同意申请入群等同为群主发送单人邀请
        } else if (offlineMessage.getOfflineMessageType() == IMSContacts.OffLineMsgType.GROUP_EXPEL) {
            // 被踢出群聊
            boolean isFromRegisteredUser = UserPreferences.isRegistered(IMSUtils.userTypeAnalysis(offlineMessage.getMsgFromId()));
            BusinessProtocolMessageStandard.Head head = BusinessProtocolMessageStandard.Head.newBuilder()
                    .setMsgType(IMSContacts.MsgType.GROUP_MEMBER_DELETE)
                    .setFromId(offlineMessage.getMsgFromId())
                    .setToId(UserPreferences.getUserId())
                    .setTimestamp(System.currentTimeMillis())
                    .build();
            BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage.Builder cMsgBuilder = BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage.newBuilder();
            if (isFromRegisteredUser) {
                cMsgBuilder.setUnifiedEntranceMessage(BusinessProtocolMessageStandard.UnifiedEntranceMessage.newBuilder()
                        .setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.GroupChatProtocol)
                        .setHead(head)
                        .setGroupChatProtocol(BusinessProtocolMessages.GroupChatProtocol.newBuilder()
                                .setUserType(IMSContacts.UserType.REGISTERED_USER)
                                .setChatMessage(offlineMessage.getChatMessage())
                                .setRegisteredGroupMember(BusinessProtocolEntities.RegisteredGroupMember.newBuilder()
                                        .setGroupMemberUserId(offlineMessage.getGroupMemberUserId())
                                        .setGroupMemberName(offlineMessage.getGroupMemberNickname())
                                        .setGroupMemberAvatar(offlineMessage.getGroupMemberAvatar())
                                        .setGroup(BusinessProtocolEntities.RegisteredGroup.newBuilder()
                                                .setGroupId(offlineMessage.getGroupId())
                                                .setGroupName(offlineMessage.getGroupName())
                                                .setGroupType(IMSUtils.userTypeAnalysis(offlineMessage.getGroupId()))
                                                .setSettings(BusinessProtocolEntities.Settings.newBuilder()
                                                        .setSnapchatStatus(offlineMessage.getSnapchatStatus())
                                                        .setSnapchatTimeLength(offlineMessage.getSnapchatTimeLength())
                                                        .build())
                                                .build())
                                        .build())
                                .build())
                        .build());
            } else {
                cMsgBuilder.setUnifiedEntranceMessage(BusinessProtocolMessageStandard.UnifiedEntranceMessage.newBuilder()
                        .setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.GroupChatProtocol)
                        .setHead(head)
                        .setGroupChatProtocol(BusinessProtocolMessages.GroupChatProtocol.newBuilder()
                                .setUserType(IMSContacts.UserType.TOURISTS_USER)
                                .setChatMessage(offlineMessage.getChatMessage())
                                .setTouristsGroupMember(BusinessProtocolEntities.TouristsGroupMember.newBuilder()
                                        .setGroupMemberUserId(offlineMessage.getGroupMemberUserId())
                                        .setGroupMemberName(offlineMessage.getGroupMemberNickname())
                                        .setGroupMemberAvatar(offlineMessage.getGroupMemberAvatar())
                                        .setGroup(BusinessProtocolEntities.TouristsGroup.newBuilder()
                                                .setGroupId(offlineMessage.getGroupId())
                                                .setGroupName(offlineMessage.getGroupName())
                                                .setGroupType(IMSUtils.userTypeAnalysis(offlineMessage.getGroupId()))
                                                .setSettings(BusinessProtocolEntities.Settings.newBuilder()
                                                        .setSnapchatStatus(offlineMessage.getSnapchatStatus())
                                                        .setSnapchatTimeLength(offlineMessage.getSnapchatTimeLength())
                                                        .build())
                                                .build())
                                        .build())
                                .build())
                        .build());
            }
            return cMsgBuilder.build();
        } else if (offlineMessage.getOfflineMessageType() == IMSContacts.OffLineMsgType.GROUP_EXIT ||
                offlineMessage.getOfflineMessageType() == IMSContacts.OffLineMsgType.GROUP_DISSOLVE) {
            // 退出群聊 or 解散群聊
            boolean isFromRegisteredUser = UserPreferences.isRegistered(IMSUtils.userTypeAnalysis(offlineMessage.getMsgFromId()));
            BusinessProtocolMessageStandard.Head head = BusinessProtocolMessageStandard.Head.newBuilder()
                    .setMsgType(IMSContacts.MsgType.GROUP_MEMBER_EXIT)
                    .setFromId(offlineMessage.getMsgFromId())
                    .setToId(UserPreferences.getUserId())
                    .setTimestamp(System.currentTimeMillis())
                    .build();
            BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage.Builder cMsgBuilder = BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage.newBuilder();
            if (isFromRegisteredUser) {
                cMsgBuilder.setUnifiedEntranceMessage(BusinessProtocolMessageStandard.UnifiedEntranceMessage.newBuilder()
                        .setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.GroupChatProtocol)
                        .setHead(head)
                        .setGroupChatProtocol(BusinessProtocolMessages.GroupChatProtocol.newBuilder()
                                .setUserType(IMSContacts.UserType.REGISTERED_USER)
                                .setChatMessage(offlineMessage.getChatMessage())
                                .setRegisteredGroupMember(BusinessProtocolEntities.RegisteredGroupMember.newBuilder()
                                        .setGroupMemberUserId(offlineMessage.getGroupMemberUserId())
                                        .setGroupMemberName(offlineMessage.getGroupMemberNickname())
                                        .setGroupMemberAvatar(offlineMessage.getGroupMemberAvatar())
                                        .setGroup(BusinessProtocolEntities.RegisteredGroup.newBuilder()
                                                .setGroupId(offlineMessage.getGroupId())
                                                .setGroupName(offlineMessage.getGroupName())
                                                .setGroupType(IMSUtils.userTypeAnalysis(offlineMessage.getGroupId()))
                                                .setSettings(BusinessProtocolEntities.Settings.newBuilder()
                                                        .setSnapchatStatus(offlineMessage.getSnapchatStatus())
                                                        .setSnapchatTimeLength(offlineMessage.getSnapchatTimeLength())
                                                        .build())
                                                .build())
                                        .build())
                                .build())
                        .build());
            } else {
                cMsgBuilder.setUnifiedEntranceMessage(BusinessProtocolMessageStandard.UnifiedEntranceMessage.newBuilder()
                        .setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.GroupChatProtocol)
                        .setHead(head)
                        .setGroupChatProtocol(BusinessProtocolMessages.GroupChatProtocol.newBuilder()
                                .setUserType(IMSContacts.UserType.TOURISTS_USER)
                                .setChatMessage(offlineMessage.getChatMessage())
                                .setTouristsGroupMember(BusinessProtocolEntities.TouristsGroupMember.newBuilder()
                                        .setGroupMemberUserId(offlineMessage.getGroupMemberUserId())
                                        .setGroupMemberName(offlineMessage.getGroupMemberNickname())
                                        .setGroupMemberAvatar(offlineMessage.getGroupMemberAvatar())
                                        .setGroup(BusinessProtocolEntities.TouristsGroup.newBuilder()
                                                .setGroupId(offlineMessage.getGroupId())
                                                .setGroupName(offlineMessage.getGroupName())
                                                .setGroupType(IMSUtils.userTypeAnalysis(offlineMessage.getGroupId()))
                                                .setSettings(BusinessProtocolEntities.Settings.newBuilder()
                                                        .setSnapchatStatus(offlineMessage.getSnapchatStatus())
                                                        .setSnapchatTimeLength(offlineMessage.getSnapchatTimeLength())
                                                        .build())
                                                .build())
                                        .build())
                                .build())
                        .build());
            }
            return cMsgBuilder.build();
        } else if (offlineMessage.getOfflineMessageType() == IMSContacts.OffLineMsgType.GROUP_MSG_DEL) {
            BusinessProtocolMessageStandard.Head head = BusinessProtocolMessageStandard.Head.newBuilder()
                    .setMsgType(IMSContacts.MsgType.DEL_GROUP_OFF_MESSAGE)
                    .setFromId(offlineMessage.getMsgFromId())
                    .setToId(UserPreferences.getUserId())
                    .setTimestamp(System.currentTimeMillis())
                    .build();
            BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage.Builder cMsgBuilder = BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage.newBuilder();
            cMsgBuilder.setUnifiedEntranceMessage(BusinessProtocolMessageStandard.UnifiedEntranceMessage.newBuilder()
                    .setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.DelGroupOffMessageProtocol)
                    .setHead(head)
                    .setDelGroupOffMessageProtocol(BusinessProtocolMessages.DelGroupOffMessageProtocol.newBuilder()
                            .setUserType(IMSContacts.UserType.REGISTERED_USER)
                            .setUserId(offlineMessage.getMsgFromId())
                            .setMsgIds(offlineMessage.getChatMessage().getMsgId())
                            .setChatTypes(offlineMessage.getGroupName())
                            .setMemberIds(offlineMessage.getGroupMemberUserId())
                            .setGroupId(offlineMessage.getGroupId())
                            .build()
                    ).build());
            return cMsgBuilder.build();
        }
        return null;
    }

    /**
     * ===================================================以下好友离线消息
     */
    public static void handleOfflineProtocol(BusinessProtocolMessages.OffFriendMessageListProtocol protocol) {
        OnDispenseResponseListener onDispenseResponseListener = TcpLengthHeartbeatBroadcastReceive.getOnDispenseResponseListener();
        if (onDispenseResponseListener == null) return;

        List<BusinessProtocolEntities.RegistFriendOffMessage> registFriendOffMessageList = protocol.getRegistFriendOffMessageList();
        for (BusinessProtocolEntities.RegistFriendOffMessage offlineMessage : registFriendOffMessageList) {
            BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage build = BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage
                    .newBuilder()
                    .setUnifiedEntranceMessage(handleOfflineMessage(protocol, offlineMessage))
                    .build();

            onDispenseResponseListener.onResponse(build.getUnifiedEntranceMessage().getHead().getMsgType(), build);
        }

        List<BusinessProtocolEntities.TouristFriendOffMessage> touristFriendOffMessageList = protocol.getTouristFriendOffMessageList();
        for (BusinessProtocolEntities.TouristFriendOffMessage offlineMessage : touristFriendOffMessageList) {
            BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage build = BusinessProtocolMessageStandard.CheckUnifiedEntranceMessage
                    .newBuilder()
                    .setUnifiedEntranceMessage(handleOfflineMessage(protocol, offlineMessage))
                    .build();

            onDispenseResponseListener.onResponse(build.getUnifiedEntranceMessage().getHead().getMsgType(), build);
        }
    }


    private static BusinessProtocolMessageStandard.UnifiedEntranceMessage handleOfflineMessage(BusinessProtocolMessages.OffFriendMessageListProtocol protocol, BusinessProtocolEntities.RegistFriendOffMessage offlineMessage) {
        BusinessProtocolMessageStandard.UnifiedEntranceMessage.Builder msgBuilder = BusinessProtocolMessageStandard.UnifiedEntranceMessage.newBuilder();
        msgBuilder.setHead(generateMessageHead(offlineMessage));
        BusinessProtocolMessageStandard.Head head = generateMessageHead(offlineMessage);
        msgBuilder.setHead(head);
        String msgType = head.getMsgType();
        int chatType = offlineMessage.getChatType();
        if (IMSContacts.MsgType.FRIEND_CHAT_TOURISTS.equals(msgType) ||
                IMSContacts.MsgType.FRIEND_APPLY_TOURISTS.equals(msgType) ||
                IMSContacts.MsgType.FRIEND_APPLY_REFUSE_TOURISTS.equals(msgType)) {
            // 游客 聊天消息，好友申请，拒绝申请
            BusinessProtocolEntities.RegisteredFriend registeredFriend = offlineMessage.getRegisteredFriend();
            msgBuilder.setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.FriendChatProtocol);
            msgBuilder.setFriendChatProtocol(BusinessProtocolMessages.FriendChatProtocol.newBuilder()
                    .setUserType(IMSContacts.UserType.TOURISTS_USER)
                    .setTouristsUser(protocol.getTouristsUser())
                    .setChatMessage(handleChatMessage(chatType, offlineMessage))
                    .setTouristsFriend(BusinessProtocolEntities.TouristsFriend.newBuilder()
                            .setFriendId(registeredFriend.getFriendId())
                            .setFriendUserId(registeredFriend.getFriendUserId())
                            .setFriendNickname(registeredFriend.getFriendNickname())
                            .setFriendAvatar(registeredFriend.getFriendAvatar())
                            .setFriendAlias(registeredFriend.getFriendAlias())
                            .setUserType(IMSUtils.userTypeAnalysis(registeredFriend.getFriendUserId()))
                            .setOnlineStatus(registeredFriend.getOnlineStatus())
                            .setSettings(registeredFriend.getSettings())
                            .build())
                    .build());
        } else if (IMSContacts.MsgType.FRIEND_CHAT_REGISTERED.equals(msgType) ||
                IMSContacts.MsgType.FRIEND_APPLY_REGISTERED.equals(msgType) ||
                IMSContacts.MsgType.FRIEND_APPLY_REFUSE_REGISTERED.equals(msgType)) {
            // 注册用户客 聊天消息，好友申请，拒绝申请
            msgBuilder.setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.FriendChatProtocol);
            msgBuilder.setFriendChatProtocol(BusinessProtocolMessages.FriendChatProtocol.newBuilder()
                    .setUserType(IMSContacts.UserType.REGISTERED_USER)
                    .setRegisteredUser(protocol.getRegisteredUser())
                    .setChatMessage(handleChatMessage(chatType, offlineMessage))
                    .setRegisteredFriend(offlineMessage.getRegisteredFriend())
                    .build());
        } else if (IMSContacts.MsgType.FRIEND_APPLY_AGREE_TOURISTS.equals(msgType)) {
            // 游客 同意好友申请
            BusinessProtocolEntities.RegisteredFriend registeredFriend = offlineMessage.getRegisteredFriend();
            msgBuilder.setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.FriendAddProtocol);
            msgBuilder.setFriendAddProtocol(BusinessProtocolMessages.FriendAddProtocol.newBuilder()
                    .setUserType(IMSContacts.UserType.TOURISTS_USER)
                    .setTouristsUser(protocol.getTouristsUser())
                    .addTouristsFriend(BusinessProtocolEntities.TouristsFriend.newBuilder()
                            .setFriendId(registeredFriend.getFriendId())
                            .setFriendUserId(registeredFriend.getFriendUserId())
                            .setFriendNickname(registeredFriend.getFriendNickname())
                            .setFriendAvatar(registeredFriend.getFriendAvatar())
                            .setFriendAlias(registeredFriend.getFriendAlias())
                            .setUserType(IMSUtils.userTypeAnalysis(registeredFriend.getFriendUserId()))
                            .setOnlineStatus(registeredFriend.getOnlineStatus())
                            .setSettings(registeredFriend.getSettings())
                            .build())
                    .setOperation(IMSContacts.Operation.AGREE)
                    .build());
        } else if (IMSContacts.MsgType.FRIEND_APPLY_AGREE_REGISTERED.equals(msgType)) {
            // 注册用户 同意好友申请
            msgBuilder.setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.FriendAddProtocol);
            msgBuilder.setFriendAddProtocol(BusinessProtocolMessages.FriendAddProtocol.newBuilder()
                    .setUserType(IMSContacts.UserType.REGISTERED_USER)
                    .setRegisteredUser(protocol.getRegisteredUser())
                    .addRegisteredFriend(offlineMessage.getRegisteredFriend())
                    .setOperation(IMSContacts.Operation.AGREE)
                    .build());
        } else if (IMSContacts.MsgType.FRIEND_DELETE_TOURISTS.equals(msgType)) {
            // 游客 删除好友
            BusinessProtocolEntities.RegisteredFriend registeredFriend = offlineMessage.getRegisteredFriend();
            msgBuilder.setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.FriendDeleteProtocol);
            msgBuilder.setFriendDeleteProtocol(BusinessProtocolMessages.FriendDeleteProtocol.newBuilder()
                    .setUserType(IMSContacts.UserType.TOURISTS_USER)
                    .setTouristsUser(protocol.getTouristsUser())
                    .setTouristsFriend(BusinessProtocolEntities.TouristsFriend.newBuilder()
                            .setFriendId(registeredFriend.getFriendId())
                            .setFriendUserId(registeredFriend.getFriendUserId())
                            .setFriendNickname(registeredFriend.getFriendNickname())
                            .setFriendAvatar(registeredFriend.getFriendAvatar())
                            .setFriendAlias(registeredFriend.getFriendAlias())
                            .setUserType(IMSUtils.userTypeAnalysis(registeredFriend.getFriendUserId()))
                            .setOnlineStatus(registeredFriend.getOnlineStatus())
                            .setSettings(registeredFriend.getSettings())
                            .build())
                    .build());
        } else if (IMSContacts.MsgType.FRIEND_DELETE_REGISTERED.equals(msgType)) {
            // 注册用户 删除好友
            msgBuilder.setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.FriendDeleteProtocol);
            msgBuilder.setFriendDeleteProtocol(BusinessProtocolMessages.FriendDeleteProtocol.newBuilder()
                    .setUserType(IMSContacts.UserType.REGISTERED_USER)
                    .setRegisteredUser(protocol.getRegisteredUser())
                    .setRegisteredFriend(offlineMessage.getRegisteredFriend())
                    .build());
        } else if (IMSContacts.MsgType.DEL_FRIEND_OFF_MESSAGE_TOURIST.equals(msgType)) {
            // 游客 删除聊天消息
            BusinessProtocolEntities.RegisteredFriend registeredFriend = offlineMessage.getRegisteredFriend();
            msgBuilder.setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.DelFriendOffMessageProtocol);
            msgBuilder.setDelFriendOffMessageProtocol(BusinessProtocolMessages.DelFriendOffMessageProtocol.newBuilder()
                    .setUserType(IMSContacts.UserType.TOURISTS_USER)
                    .setTouristsUser(protocol.getTouristsUser())
                    .setChatMessage(handleChatMessage(chatType, offlineMessage))
                    .setTouristsFriend(BusinessProtocolEntities.TouristsFriend.newBuilder()
                            .setFriendId(registeredFriend.getFriendId())
                            .setFriendUserId(registeredFriend.getFriendUserId())
                            .setFriendNickname(registeredFriend.getFriendNickname())
                            .setFriendAvatar(registeredFriend.getFriendAvatar())
                            .setFriendAlias(registeredFriend.getFriendAlias())
                            .setUserType(IMSUtils.userTypeAnalysis(registeredFriend.getFriendUserId()))
                            .setOnlineStatus(registeredFriend.getOnlineStatus())
                            .setSettings(registeredFriend.getSettings())
                            .build())
                    .setMsgfromId(offlineMessage.getMsgFromUserId())
                    .build());
        } else if (IMSContacts.MsgType.DEL_FRIEND_OFF_MESSAGE_REGIST.equals(msgType)) {
            // 注册用户 删除聊天消息
            msgBuilder.setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.DelFriendOffMessageProtocol);
            msgBuilder.setDelFriendOffMessageProtocol(BusinessProtocolMessages.DelFriendOffMessageProtocol.newBuilder()
                    .setUserType(IMSContacts.UserType.REGISTERED_USER)
                    .setRegisteredUser(protocol.getRegisteredUser())
                    .setChatMessage(handleChatMessage(chatType, offlineMessage))
                    .setRegisteredFriend(offlineMessage.getRegisteredFriend())
                    .setMsgfromId(offlineMessage.getMsgFromUserId())
                    .build());
        }
        return msgBuilder.build();
    }

    private static BusinessProtocolMessageStandard.UnifiedEntranceMessage handleOfflineMessage(BusinessProtocolMessages.OffFriendMessageListProtocol protocol, BusinessProtocolEntities.TouristFriendOffMessage offlineMessage) {
        BusinessProtocolMessageStandard.UnifiedEntranceMessage.Builder msgBuilder = BusinessProtocolMessageStandard.UnifiedEntranceMessage.newBuilder();
        BusinessProtocolMessageStandard.Head head = generateMessageHead(offlineMessage);
        msgBuilder.setHead(head);
        String msgType = head.getMsgType();
        int chatType = offlineMessage.getChatType();
        if (IMSContacts.MsgType.FRIEND_CHAT_TOURISTS.equals(msgType) ||
                IMSContacts.MsgType.FRIEND_APPLY_TOURISTS.equals(msgType) ||
                IMSContacts.MsgType.FRIEND_APPLY_REFUSE_TOURISTS.equals(msgType)) {
            // 游客 聊天消息，好友申请，拒绝申请
            msgBuilder.setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.FriendChatProtocol);
            msgBuilder.setFriendChatProtocol(BusinessProtocolMessages.FriendChatProtocol.newBuilder()
                    .setUserType(IMSContacts.UserType.TOURISTS_USER)
                    .setTouristsUser(protocol.getTouristsUser())
                    .setChatMessage(handleChatMessage(chatType, offlineMessage))
                    .setTouristsFriend(offlineMessage.getTouristsFriend())
                    .build());
        } else if (IMSContacts.MsgType.FRIEND_CHAT_REGISTERED.equals(msgType) ||
                IMSContacts.MsgType.FRIEND_APPLY_REGISTERED.equals(msgType) ||
                IMSContacts.MsgType.FRIEND_APPLY_REFUSE_REGISTERED.equals(msgType)) {
            // 注册用户客 聊天消息，好友申请，拒绝申请
            BusinessProtocolEntities.TouristsFriend touristsFriend = offlineMessage.getTouristsFriend();
            msgBuilder.setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.FriendChatProtocol);
            msgBuilder.setFriendChatProtocol(BusinessProtocolMessages.FriendChatProtocol.newBuilder()
                    .setUserType(IMSContacts.UserType.REGISTERED_USER)
                    .setRegisteredUser(protocol.getRegisteredUser())
                    .setChatMessage(handleChatMessage(chatType, offlineMessage))
                    .setRegisteredFriend(BusinessProtocolEntities.RegisteredFriend.newBuilder()
                            .setFriendId(touristsFriend.getFriendId())
                            .setFriendUserId(touristsFriend.getFriendUserId())
                            .setFriendNickname(touristsFriend.getFriendNickname())
                            .setFriendAvatar(touristsFriend.getFriendAvatar())
                            .setFriendAlias(touristsFriend.getFriendAlias())
                            .setUserType(IMSUtils.userTypeAnalysis(touristsFriend.getFriendUserId()))
                            .setOnlineStatus(touristsFriend.getOnlineStatus())
                            .setSettings(touristsFriend.getSettings())
                            .build())
                    .build());
        } else if (IMSContacts.MsgType.FRIEND_APPLY_AGREE_TOURISTS.equals(msgType)) {
            // 游客 同意好友申请
            msgBuilder.setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.FriendAddProtocol);
            msgBuilder.setFriendAddProtocol(BusinessProtocolMessages.FriendAddProtocol.newBuilder()
                    .setUserType(IMSContacts.UserType.TOURISTS_USER)
                    .setTouristsUser(protocol.getTouristsUser())
                    .addTouristsFriend(offlineMessage.getTouristsFriend())
                    .setOperation(IMSContacts.Operation.AGREE)
                    .build());
        } else if (IMSContacts.MsgType.FRIEND_APPLY_AGREE_REGISTERED.equals(msgType)) {
            // 注册用户 同意好友申请
            BusinessProtocolEntities.TouristsFriend touristsFriend = offlineMessage.getTouristsFriend();
            msgBuilder.setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.FriendAddProtocol);
            msgBuilder.setFriendAddProtocol(BusinessProtocolMessages.FriendAddProtocol.newBuilder()
                    .setUserType(IMSContacts.UserType.REGISTERED_USER)
                    .setRegisteredUser(protocol.getRegisteredUser())
                    .addRegisteredFriend(BusinessProtocolEntities.RegisteredFriend.newBuilder()
                            .setFriendId(touristsFriend.getFriendId())
                            .setFriendUserId(touristsFriend.getFriendUserId())
                            .setFriendNickname(touristsFriend.getFriendNickname())
                            .setFriendAvatar(touristsFriend.getFriendAvatar())
                            .setFriendAlias(touristsFriend.getFriendAlias())
                            .setUserType(IMSUtils.userTypeAnalysis(touristsFriend.getFriendUserId()))
                            .setOnlineStatus(touristsFriend.getOnlineStatus())
                            .setSettings(touristsFriend.getSettings())
                            .build())
                    .setOperation(IMSContacts.Operation.AGREE)
                    .build());
        } else if (IMSContacts.MsgType.FRIEND_DELETE_TOURISTS.equals(msgType)) {
            // 游客 删除好友
            msgBuilder.setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.FriendDeleteProtocol);
            msgBuilder.setFriendDeleteProtocol(BusinessProtocolMessages.FriendDeleteProtocol.newBuilder()
                    .setUserType(IMSContacts.UserType.TOURISTS_USER)
                    .setTouristsUser(protocol.getTouristsUser())
                    .setTouristsFriend(offlineMessage.getTouristsFriend())
                    .build());
        } else if (IMSContacts.MsgType.FRIEND_DELETE_REGISTERED.equals(msgType)) {
            // 注册用户 删除好友
            BusinessProtocolEntities.TouristsFriend touristsFriend = offlineMessage.getTouristsFriend();
            msgBuilder.setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.FriendDeleteProtocol);
            msgBuilder.setFriendDeleteProtocol(BusinessProtocolMessages.FriendDeleteProtocol.newBuilder()
                    .setUserType(IMSContacts.UserType.REGISTERED_USER)
                    .setRegisteredUser(protocol.getRegisteredUser())
                    .setRegisteredFriend(BusinessProtocolEntities.RegisteredFriend.newBuilder()
                            .setFriendId(touristsFriend.getFriendId())
                            .setFriendUserId(touristsFriend.getFriendUserId())
                            .setFriendNickname(touristsFriend.getFriendNickname())
                            .setFriendAvatar(touristsFriend.getFriendAvatar())
                            .setFriendAlias(touristsFriend.getFriendAlias())
                            .setUserType(IMSUtils.userTypeAnalysis(touristsFriend.getFriendUserId()))
                            .setOnlineStatus(touristsFriend.getOnlineStatus())
                            .setSettings(touristsFriend.getSettings())
                            .build())
                    .build());
        } else if (IMSContacts.MsgType.DEL_FRIEND_OFF_MESSAGE_TOURIST.equals(msgType)) {
            // 游客 删除聊天消息
            msgBuilder.setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.DelFriendOffMessageProtocol);
            msgBuilder.setDelFriendOffMessageProtocol(BusinessProtocolMessages.DelFriendOffMessageProtocol.newBuilder()
                    .setUserType(IMSContacts.UserType.TOURISTS_USER)
                    .setTouristsUser(protocol.getTouristsUser())
                    .setChatMessage(handleChatMessage(chatType, offlineMessage))
                    .setTouristsFriend(offlineMessage.getTouristsFriend())
                    .setMsgfromId(offlineMessage.getMsgFromUserId())
                    .build());
        } else if (IMSContacts.MsgType.DEL_FRIEND_OFF_MESSAGE_REGIST.equals(msgType)) {
            // 注册用户 删除聊天消息
            BusinessProtocolEntities.TouristsFriend touristsFriend = offlineMessage.getTouristsFriend();
            msgBuilder.setDataType(BusinessProtocolMessageStandard.UnifiedEntranceMessage.DataType.DelFriendOffMessageProtocol);
            msgBuilder.setDelFriendOffMessageProtocol(BusinessProtocolMessages.DelFriendOffMessageProtocol.newBuilder()
                    .setUserType(IMSContacts.UserType.REGISTERED_USER)
                    .setRegisteredUser(protocol.getRegisteredUser())
                    .setChatMessage(handleChatMessage(chatType, offlineMessage))
                    .setRegisteredFriend(BusinessProtocolEntities.RegisteredFriend.newBuilder()
                            .setFriendId(touristsFriend.getFriendId())
                            .setFriendUserId(touristsFriend.getFriendUserId())
                            .setFriendNickname(touristsFriend.getFriendNickname())
                            .setFriendAvatar(touristsFriend.getFriendAvatar())
                            .setFriendAlias(touristsFriend.getFriendAlias())
                            .setUserType(IMSUtils.userTypeAnalysis(touristsFriend.getFriendUserId()))
                            .setOnlineStatus(touristsFriend.getOnlineStatus())
                            .setSettings(touristsFriend.getSettings())
                            .build())
                    .setMsgfromId(offlineMessage.getMsgFromUserId())
                    .build());
        }
        return msgBuilder.build();
    }

    /**
     * Head
     *
     * @param offlineMessage
     * @return
     */
    private static BusinessProtocolMessageStandard.Head generateMessageHead(BusinessProtocolEntities.RegistFriendOffMessage offlineMessage) {
        BusinessProtocolMessageStandard.Head.Builder headBuilder = BusinessProtocolMessageStandard.Head.newBuilder();
        int chatType = offlineMessage.getChatType();
        if (chatType == IMSContacts.MsgContentType.TEXT ||
                chatType == IMSContacts.MsgContentType.PICTURE ||
                chatType == IMSContacts.MsgContentType.VIDEO ||
                chatType == IMSContacts.MsgContentType.VOICE ||
                chatType == IMSContacts.MsgContentType.FILE) {
            // 好友聊天消息
            headBuilder.setMsgType(IMSContacts.MsgType.FRIEND_CHAT_REGISTERED);
            headBuilder.setFromId(offlineMessage.getRegisteredFriend().getFriendUserId());
            headBuilder.setToId(UserPreferences.getUserId());
            String msgId = offlineMessage.getMsgId();
            headBuilder.setTimestamp(ChatUtils.analysisTimestampInMessageId(msgId));
            headBuilder.setUniqueIdentify(msgId);
        } else if (chatType == IMSContacts.MsgContentType.APPLY_FRIEND_TOURISTS ||
                chatType == IMSContacts.MsgContentType.APPLY_FRIEND_REGISTERED) {
            // 好友申请
            headBuilder.setMsgType(chatType == IMSContacts.MsgContentType.APPLY_FRIEND_TOURISTS ? IMSContacts.MsgType.FRIEND_APPLY_TOURISTS : IMSContacts.MsgType.FRIEND_APPLY_REGISTERED);
            headBuilder.setFromId(offlineMessage.getRegisteredFriend().getFriendUserId());
            headBuilder.setToId(UserPreferences.getUserId());
        } else if (chatType == IMSContacts.MsgContentType.APPLY_REFUSE_FRIEND_TOURISTS ||
                chatType == IMSContacts.MsgContentType.APPLY_REFUSE_FRIEND_REGISTERED) {
            // 好友申请拒绝
            headBuilder.setMsgType(chatType == IMSContacts.MsgContentType.APPLY_REFUSE_FRIEND_TOURISTS ? IMSContacts.MsgType.FRIEND_APPLY_REFUSE_TOURISTS : IMSContacts.MsgType.FRIEND_APPLY_REFUSE_REGISTERED);
            headBuilder.setFromId(offlineMessage.getRegisteredFriend().getFriendUserId());
            headBuilder.setToId(UserPreferences.getUserId());
        } else if (chatType == IMSContacts.MsgContentType.APPLY_AGREE_FRIEND_TOURISTS ||
                chatType == IMSContacts.MsgContentType.APPLY_AGREE_FRIEND_REGISTERED) {
            // 好友申请同意
            headBuilder.setMsgType(chatType == IMSContacts.MsgContentType.APPLY_AGREE_FRIEND_TOURISTS ? IMSContacts.MsgType.FRIEND_APPLY_AGREE_TOURISTS : IMSContacts.MsgType.FRIEND_APPLY_AGREE_REGISTERED);
            headBuilder.setFromId(offlineMessage.getRegisteredFriend().getFriendUserId());
            headBuilder.setToId(UserPreferences.getUserId());
        } else if (chatType == IMSContacts.MsgContentType.DELETE_FRIEND_TOURISTS ||
                chatType == IMSContacts.MsgContentType.DELETE_FRIEND_REGISTERED) {
            // 删除好友
            headBuilder.setMsgType(chatType == IMSContacts.MsgContentType.DELETE_FRIEND_TOURISTS ? IMSContacts.MsgType.FRIEND_DELETE_TOURISTS : IMSContacts.MsgType.FRIEND_DELETE_REGISTERED);
            headBuilder.setFromId(offlineMessage.getRegisteredFriend().getFriendUserId());
            headBuilder.setToId(UserPreferences.getUserId());
        } else if (chatType == IMSContacts.MsgContentType.DEL_OFF_MESSAGE_TOURISTS ||
                chatType == IMSContacts.MsgContentType.DEL_OFF_MESSAGE_REGISTERED) {
            headBuilder.setMsgType(chatType == IMSContacts.MsgContentType.DEL_OFF_MESSAGE_TOURISTS ? IMSContacts.MsgType.DEL_FRIEND_OFF_MESSAGE_TOURIST : IMSContacts.MsgType.DEL_FRIEND_OFF_MESSAGE_REGIST);
            headBuilder.setFromId(offlineMessage.getRegisteredFriend().getFriendUserId());
            headBuilder.setToId(UserPreferences.getUserId());
        }
        return headBuilder.build();
    }

    private static BusinessProtocolMessageStandard.Head generateMessageHead(BusinessProtocolEntities.TouristFriendOffMessage offlineMessage) {
        BusinessProtocolMessageStandard.Head.Builder headBuilder = BusinessProtocolMessageStandard.Head.newBuilder();
        int chatType = offlineMessage.getChatType();
        if (chatType == IMSContacts.MsgContentType.TEXT ||
                chatType == IMSContacts.MsgContentType.PICTURE ||
                chatType == IMSContacts.MsgContentType.VIDEO ||
                chatType == IMSContacts.MsgContentType.VOICE ||
                chatType == IMSContacts.MsgContentType.FILE) {
            // 好友聊天消息
            headBuilder.setMsgType(IMSContacts.MsgType.FRIEND_CHAT_REGISTERED);
            headBuilder.setFromId(offlineMessage.getTouristsFriend().getFriendUserId());
            headBuilder.setToId(UserPreferences.getUserId());
            String msgId = offlineMessage.getMsgId();
            headBuilder.setTimestamp(ChatUtils.analysisTimestampInMessageId(msgId));
            headBuilder.setUniqueIdentify(msgId);
        } else if (chatType == IMSContacts.MsgContentType.APPLY_FRIEND_TOURISTS ||
                chatType == IMSContacts.MsgContentType.APPLY_FRIEND_REGISTERED) {
            // 好友申请
            headBuilder.setMsgType(chatType == IMSContacts.MsgContentType.APPLY_FRIEND_TOURISTS ? IMSContacts.MsgType.FRIEND_APPLY_TOURISTS : IMSContacts.MsgType.FRIEND_APPLY_REGISTERED);
            headBuilder.setFromId(offlineMessage.getTouristsFriend().getFriendUserId());
            headBuilder.setToId(UserPreferences.getUserId());
        } else if (chatType == IMSContacts.MsgContentType.APPLY_REFUSE_FRIEND_TOURISTS ||
                chatType == IMSContacts.MsgContentType.APPLY_REFUSE_FRIEND_REGISTERED) {
            // 好友申请拒绝
            headBuilder.setMsgType(chatType == IMSContacts.MsgContentType.APPLY_REFUSE_FRIEND_TOURISTS ? IMSContacts.MsgType.FRIEND_APPLY_REFUSE_TOURISTS : IMSContacts.MsgType.FRIEND_APPLY_REFUSE_REGISTERED);
            headBuilder.setFromId(offlineMessage.getTouristsFriend().getFriendUserId());
            headBuilder.setToId(UserPreferences.getUserId());
        } else if (chatType == IMSContacts.MsgContentType.APPLY_AGREE_FRIEND_TOURISTS ||
                chatType == IMSContacts.MsgContentType.APPLY_AGREE_FRIEND_REGISTERED) {
            // 好友申请同意
            headBuilder.setMsgType(chatType == IMSContacts.MsgContentType.APPLY_AGREE_FRIEND_TOURISTS ? IMSContacts.MsgType.FRIEND_APPLY_AGREE_TOURISTS : IMSContacts.MsgType.FRIEND_APPLY_AGREE_REGISTERED);
            headBuilder.setFromId(offlineMessage.getTouristsFriend().getFriendUserId());
            headBuilder.setToId(UserPreferences.getUserId());
        } else if (chatType == IMSContacts.MsgContentType.DELETE_FRIEND_TOURISTS ||
                chatType == IMSContacts.MsgContentType.DELETE_FRIEND_REGISTERED) {
            // 删除好友
            headBuilder.setMsgType(chatType == IMSContacts.MsgContentType.DELETE_FRIEND_TOURISTS ? IMSContacts.MsgType.FRIEND_DELETE_TOURISTS : IMSContacts.MsgType.FRIEND_DELETE_REGISTERED);
            headBuilder.setFromId(offlineMessage.getTouristsFriend().getFriendUserId());
            headBuilder.setToId(UserPreferences.getUserId());
        } else if (chatType == IMSContacts.MsgContentType.DEL_OFF_MESSAGE_TOURISTS ||
                chatType == IMSContacts.MsgContentType.DEL_OFF_MESSAGE_REGISTERED) {
            headBuilder.setMsgType(chatType == IMSContacts.MsgContentType.DEL_OFF_MESSAGE_TOURISTS ? IMSContacts.MsgType.DEL_FRIEND_OFF_MESSAGE_TOURIST : IMSContacts.MsgType.DEL_FRIEND_OFF_MESSAGE_REGIST);
            headBuilder.setFromId(offlineMessage.getTouristsFriend().getFriendUserId());
            headBuilder.setToId(UserPreferences.getUserId());
        }
        return headBuilder.build();
    }

    /**
     * ChatMessage
     *
     * @param chatType
     * @param offlineMessage
     * @return
     */
    private static BusinessProtocolEntities.ChatMessage handleChatMessage(int chatType, BusinessProtocolEntities.RegistFriendOffMessage offlineMessage) {
        BusinessProtocolEntities.ChatMessage.Builder builder = BusinessProtocolEntities.ChatMessage.newBuilder();
        if (chatType == IMSContacts.MsgContentType.TEXT
                || chatType == IMSContacts.MsgContentType.APPLY_FRIEND_TOURISTS             // 申请加好友
                || chatType == IMSContacts.MsgContentType.APPLY_FRIEND_REGISTERED           // 申请加好友
                || chatType == IMSContacts.MsgContentType.APPLY_REFUSE_FRIEND_TOURISTS      // 拒绝申请加好友
                || chatType == IMSContacts.MsgContentType.APPLY_REFUSE_FRIEND_REGISTERED   // 拒绝申请加好友
        ) {
            builder.setMsgId(offlineMessage.getMsgId())
                    .setChatType(chatType)
                    .setText(offlineMessage.getText());
        } else if (chatType == IMSContacts.MsgContentType.PICTURE) {
            builder.setMsgId(offlineMessage.getMsgId())
                    .setChatType(chatType)
                    .setPicture(offlineMessage.getPicture());
        } else if (chatType == IMSContacts.MsgContentType.VIDEO) {
            builder.setMsgId(offlineMessage.getMsgId())
                    .setChatType(chatType)
                    .setVideo(offlineMessage.getVideo());
        } else if (chatType == IMSContacts.MsgContentType.VOICE) {
            builder.setMsgId(offlineMessage.getMsgId())
                    .setChatType(chatType)
                    .setVoice(offlineMessage.getVoice());
        } else if (chatType == IMSContacts.MsgContentType.FILE) {
            builder.setMsgId(offlineMessage.getMsgId())
                    .setChatType(chatType)
                    .setFile(offlineMessage.getFile());
        } else if (chatType == IMSContacts.MsgContentType.DEL_OFF_MESSAGE_TOURISTS
                || chatType == IMSContacts.MsgContentType.DEL_OFF_MESSAGE_REGISTERED) {
            builder.setMsgId(offlineMessage.getMsgId());
        }
        return builder.build();
    }

    private static BusinessProtocolEntities.ChatMessage handleChatMessage(int chatType, BusinessProtocolEntities.TouristFriendOffMessage offlineMessage) {
        BusinessProtocolEntities.ChatMessage.Builder builder = BusinessProtocolEntities.ChatMessage.newBuilder();
        if (chatType == IMSContacts.MsgContentType.TEXT
                || chatType == IMSContacts.MsgContentType.APPLY_FRIEND_TOURISTS             // 申请加好友
                || chatType == IMSContacts.MsgContentType.APPLY_FRIEND_REGISTERED           // 申请加好友
                || chatType == IMSContacts.MsgContentType.APPLY_REFUSE_FRIEND_TOURISTS      // 拒绝申请加好友
                || chatType == IMSContacts.MsgContentType.APPLY_REFUSE_FRIEND_REGISTERED   // 拒绝申请加好友
        ) {
            builder.setMsgId(offlineMessage.getMsgId())
                    .setChatType(chatType)
                    .setText(offlineMessage.getText());
        } else if (chatType == IMSContacts.MsgContentType.PICTURE) {
            builder.setMsgId(offlineMessage.getMsgId())
                    .setChatType(chatType)
                    .setPicture(offlineMessage.getPicture());
        } else if (chatType == IMSContacts.MsgContentType.VIDEO) {
            builder.setMsgId(offlineMessage.getMsgId())
                    .setChatType(chatType)
                    .setVideo(offlineMessage.getVideo());
        } else if (chatType == IMSContacts.MsgContentType.VOICE) {
            builder.setMsgId(offlineMessage.getMsgId())
                    .setChatType(chatType)
                    .setVoice(offlineMessage.getVoice());
        } else if (chatType == IMSContacts.MsgContentType.FILE) {
            builder.setMsgId(offlineMessage.getMsgId())
                    .setChatType(chatType)
                    .setVoice(offlineMessage.getVoice());
        } else if (chatType == IMSContacts.MsgContentType.DEL_OFF_MESSAGE_TOURISTS
                || chatType == IMSContacts.MsgContentType.DEL_OFF_MESSAGE_REGISTERED) {
            builder.setMsgId(offlineMessage.getMsgId());
        }
        return builder.build();
    }
}
